<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">

<head>
    <title>主页</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>

<body background="../static/images/mainbg.png" th:background="@{/images/mainbg.png}">

    <div style="float: right" >
        <button type="button" id="logoutBtn" class="btn mr-5 bg-light">退出登录</button>
    </div>

    <div class="container mt-5">
        <div class="text-center">
            <h1><b>登录成功</b></h1>
        </div>

        <div class="card mx-auto" style="width:400px">
            <img class="card-img-top" src="https://static.runoob.com/images/mix/img_avatar.png" alt="Card image"
                style="width:100%">
            <div class="card-body">
                <h4 id="nickName" class="card-title">大帅</h4>
                <p class="card-text">This guy is cool. He didn't leave anything.</p>
            </div>
        </div>
        <br>

    </div>

    <script>
        $(document).ready(function () {
            
            $('#logoutBtn').click(function () {
                $.get("api/logout", function (res) {
                    if (res == true) {
                        $(location).attr('href', '/')
                    }
                })
            })

            let jsonStr = localStorage.getItem("user")
            let user = JSON.parse(jsonStr)

            $.post("api/getUserInfo", {
                id: user.id
            }, function (res) {
                console.log(res)
                $('#nickName').text(res.data.nickName)
                localStorage.removeItem("user")
            })

            let delay = 0;
            let handler = setInterval(function () {

                if (delay === 0) {
                    delay = 1;
                    return
                }

                $.get("api/isLogin", function (res) {
                    if (res.data !== true) {
                        clearInterval(handler)
                        alert("您的账号已经在别处登录！")
                        $(location).attr("href", "/")
                    }
                })
            }, 3000)

        })
    </script>
</body>

</html>